<?php

/**
 * This is the model class for table "tbl_course".
 *
 * The followings are the available columns in table 'tbl_course':
 * @property string $id
 * @property string $parent_name
 * @property string $home_number
 * @property string $street
 * @property string $id_district
 * @property string $cell_phone
 * @property string $table_phone
 * @property string $student_name
 * @property string $id_level
 * @property integer $sex
 * @property string $ability
 * @property integer $tutor_request
 * @property integer $sex_request
 * @property string $salary
 * @property string $other_request
 * @property integer $time_number
 * @property string $start_day
 * @property string $limit_date
 * @property integer $person_number
 * @property integer $is_arrange
 * @property integer $status
 * @property integer $is_printed
 * @property integer $is_getted
 * @property string $code
 * @property string $school_name
 * @property string $schedule_pattern
 * @property string $id_intropage_template
 * @property integer $date1
 * @property integer $date2
 * @property integer $date3
 * @property integer $date4
 * @property integer $date5
 * @property integer $date6
 * @property integer $date7
 * @property integer $date8
 * @property integer $date9
 * @property integer $date10
 * @property integer $date11
 * @property integer $date12
 * @property integer $date13
 * @property integer $date14
 * @property integer $date15
 * @property integer $date16
 * @property integer $date17
 * @property integer $date18
 * @property integer $date19
 * @property integer $date20
 * @property integer $date21
 * @property string $time1
 * @property string $time2
 * @property string $time3
 * @property string $time4
 * @property string $time5
 * @property string $time6
 * @property string $time7
 * @property string $time8
 * @property string $time9
 * @property string $time10
 * @property string $time11
 * @property string $time12
 * @property string $time13
 * @property string $time14
 * @property string $time15
 * @property string $time16
 * @property string $time17
 * @property string $time18
 * @property string $time19
 * @property string $time20
 * @property string $time21
 */
class Course extends CActiveRecord
{
    public $subjectIds = array();
	/**
	 * Returns the static model of the specified AR class.
	 * @return Course the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_course';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('parent_name, home_number, street, id_district, tutor_request, sex_request', 'required'),
			array('sex, tutor_request, sex_request, time_number, person_number, is_arrange, status, is_printed, is_getted, date1, date2, date3, date4, date5, date6, date7, date8, date9, date10, date11, date12, date13, date14, date15, date16, date17, date18, date19, date20, date21', 'numerical', 'integerOnly'=>true),
			array('parent_name, student_name, code, school_name, schedule_pattern', 'length', 'max'=>200),
			array('home_number', 'length', 'max'=>50),
			array('street, id_intropage_template', 'length', 'max'=>250),
			array('id_district, id_level', 'length', 'max'=>10),
			array('cell_phone, table_phone, ability, salary, time1, time2, time3, time4, time5, time6, time7, time8, time9, time10, time11, time12, time13, time14, time15, time16, time17, time18, time19, time20, time21', 'length', 'max'=>20),
			array('other_request, start_day, limit_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, parent_name, home_number, street, id_district, cell_phone, table_phone, student_name, id_level, sex, ability, tutor_request, sex_request, salary, other_request, time_number, start_day, limit_date, person_number, is_arrange, status, is_printed, is_getted, code, school_name, schedule_pattern, date1, date2, date3, date4, date5, date6, date7, date8, date9, date10, date11, date12, date13, date14, date15, date16, date17, date18, date19, date20, date21, time1, time2, time3, time4, time5, time6, time7, time8, time9, time10, time11, time12, time13, time14, time15, time16, time17, time18, time19, time20, time21', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'intropage_template' => array(self::BELONGS_TO, 'IntropageTemplate', 'id_intropage_template'),
			'district' => array(self::BELONGS_TO, 'District', 'id_district'),
			'level' => array(self::BELONGS_TO, 'Level', 'id_level'),
			'subjects' => array(self::MANY_MANY, 'Subject', 'tbl_course_subject(id_course, id_subject)'),
			'intropages' => array(self::HAS_MANY, 'Intropage', 'id_course'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'parent_name' => 'Phụ huynh',
			'home_number' => 'Số nhà',
			'street' => 'Đường',
			'id_district' => 'Mã quận',
			'cell_phone' => 'Di động',
			'table_phone' => 'ÐT bàn',
			'student_name' => 'Học sinh',
			'id_level' => 'Mã lớp',
			'sex' => 'Giới tính',
			'ability' => 'Học lực',
			'tutor_request' => 'Yeu cầu giáo viên',
			'sex_request' => 'Yêu cầu giới tính',
			'salary' => 'Mức luong',
			'other_request' => 'Yêu cầu khác',
			'time_number' => 'Số buổi',
			'start_day' => 'Ngày bắt đầu',
			'limit_date' => 'Hạn ngày',
			'person_number' => 'Số nguời học',
			'is_arrange' => 'Sắp xếp',
			'status' => 'Trang thái',
			'is_printed' => 'Ðã in',
			'is_getted' => 'Ðã được nhận',
			'code' => 'Mã lớp',
			'school_name' => 'Tên truờng',
			'schedule_pattern' => 'Mẫu TKB',
			'id_intropage_template' => 'Mẫu giấy giới thiệu',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);

		$criteria->compare('parent_name',$this->parent_name,true);

		$criteria->compare('home_number',$this->home_number,true);

		$criteria->compare('street',$this->street,true);

		$criteria->compare('id_district',$this->id_district,true);

		$criteria->compare('cell_phone',$this->cell_phone,true);

		$criteria->compare('table_phone',$this->table_phone,true);

		$criteria->compare('student_name',$this->student_name,true);

		$criteria->compare('id_level',$this->id_level,true);

		$criteria->compare('sex',$this->sex);

		$criteria->compare('ability',$this->ability,true);

		$criteria->compare('tutor_request',$this->tutor_request);

		$criteria->compare('sex_request',$this->sex_request);

		$criteria->compare('salary',$this->salary,true);

		$criteria->compare('other_request',$this->other_request,true);

		$criteria->compare('time_number',$this->time_number);

		$criteria->compare('start_day',$this->start_day,true);

		$criteria->compare('limit_date',$this->limit_date,true);

		$criteria->compare('person_number',$this->person_number);

		$criteria->compare('is_arrange',$this->is_arrange);

		$criteria->compare('status',$this->status);

		$criteria->compare('is_printed',$this->is_printed);

		$criteria->compare('is_getted',$this->is_getted);

                if (empty($this->subjectIds) === false) {
                    $criteria->join = 'INNER JOIN tbl_course_subject cs ON cs.id_subject = t.id';
                    $criteria->join .= ' INNER JOIN tbl_subject s ON s.id = cs.id_subject';
                    $criteria->compare('s.id', $this->subjectIds, true);
                }
		return new CActiveDataProvider('Course', array(
			'criteria'=>$criteria,
                        'pagination'=>array(
                            'pageSize'=>10,
                    ),
		));
	}
        protected function afterSave() {
        parent::afterSave();
        $oldSubjects = $this->subjects;

        if (is_array($oldSubjects) && count($oldSubjects)) {
            $oldSubjects = CHtml::listData($this->subjects, 'id', 'id');
        } else {
            $oldSubjects = array();
        }

        if (is_array($this->subjectIds) && count($this->subjectIds))
        {
            $deleteSubjects = array_diff($oldSubjects, $this->subjectIds);
            $newSubjects = array_diff($this->subjectIds, $oldSubjects);
            $db = $this->getDbConnection();
            if (is_array($newSubjects)&&count($newSubjects))
            {

                foreach ($newSubjects as $subjectId)
                {
                    $command = $db->createCommand('INSERT INTO tbl_course_subject(id_course, id_subject) VALUES(:id_course, :id_subject)');
                    $command->bindValue(':id_subject', $subjectId);
                    $command->bindValue(':id_course', $this->id);
                    $command->execute();
                }
            }
        }
        elseif (count($oldSubjects))
        {
            //delete all old authors
            $deleteSubjects = $oldSubjects;
        }
         else {
            $deleteSubjects = array();
        }
        //delete
        if (count($deleteSubjects))
        {
            $db = $this->getDbConnection();
            foreach ($deleteSubjects as $subjectId)
            {
                $command = $db->createCommand('DELETE FROM tbl_course_subject WHERE id_course=:id_course AND id_subject=:id_subject');
                $command->bindValue(':id_course', $this->id);
                $command->bindValue(':id_subject', $subjectId);
                $command->execute();
            }
        }
    }

    protected function afterFind() {
        $this->subjectIds = $this->subjects;
        if (is_array($this->subjectIds) && count($this->subjectIds)) {
            $this->subjectIds = CHtml::listData($this->subjectIds, 'id', 'id');
        } else {
            $this->subjectIds = array();
        }
    }

    public function getSubjectNames()
    {
        $subjects = array();
        if (is_array($this->subjects) && count($this->subjects))
        {
            $subjects = CHtml::listData($this->subjects, 'id', 'name');
            return implode(', ', $subjects);
        }
        return '';
    }
    public function getAddress() {
        return $this->home_number." ".$this->street." ".$this->district->name;
    }
    public function getAddressNotHomeNumber() {
        return $this->street." ".$this->district->name;
    }
    public function getSubjects() {
        $subjects = "";
        foreach ($this->subjects as $subject) {
                $subjects.= ' '.CHtml::encode($subject->name);
            }
        return $subjects;
    }
    public function getSchedule() {
        $schedule = "";

        $schedule_morning = "";
        $schedule_afternoon = "";
        $schedule_evening = "";
        
        $time_morning = 0;
        $time_afternoon = 0;
        $time_evening = 0;
        //----------morning---------------------
        if($this->date1 == 1) {
            $time_morning = 1;
            $schedule_morning.= ' '.CHtml::encode("T2");
        }
        if($this->date2 == 1) {
            $time_morning = 1;
            $schedule_morning.= ' '.CHtml::encode("T3");
        }
        if($this->date3 == 1) {
            $time_morning = 1;
            $schedule_morning.= ' '.CHtml::encode("T4");
        }
        if($this->date4 == 1) {
            $time_morning = 1;
            $schedule_morning.= ' '.CHtml::encode("T5");
        }
        if($this->date5 == 1) {
            $time_morning = 1;
            $schedule_morning.= ' '.CHtml::encode("T6");
        }
        if($this->date6 == 1) {
            $time_morning = 1;
            $schedule_morning.= ' '.CHtml::encode("T7");
        }
        if($this->date7 == 1) {
            $time_morning = 1;
            $schedule_morning.= ' '.CHtml::encode("CN");
        }

        if($time_morning == 1) {
            $schedule = "Sáng: ".$schedule_morning;
        }
        //----------afternoon---------------------
        if($this->date8 == 1) {
            $time_afternoon = 1;
            $schedule_afternoon.= ' '.CHtml::encode("T2");
        }
        if($this->date9 == 1) {
            $time_afternoon = 1;
            $schedule_afternoon.= ' '.CHtml::encode("T3");
        }
        if($this->date10 == 1) {
            $time_afternoon = 1;
            $schedule_afternoon.= ' '.CHtml::encode("T4");
        }
        if($this->date11 == 1) {
            $time_afternoon = 1;
            $schedule_afternoon.= ' '.CHtml::encode("T5");
        }
        if($this->date12 == 1) {
            $time_afternoon = 1;
            $schedule_afternoon.= ' '.CHtml::encode("T6");
        }
        if($this->date13 == 1) {
            $time_afternoon = 1;
            $schedule_afternoon.= ' '.CHtml::encode("T7");
        }
        if($this->date14 == 1) {
            $time_afternoon = 1;
            $schedule_afternoon.= ' '.CHtml::encode("CN");
        }

        if($time_afternoon == 1) {
            $schedule .= " Chiều: ".$schedule_afternoon;
        }
        //----------evening---------------------
        if($this->date15 == 1) {
            $time_evening = 1;
            $schedule_evening.= ' '.CHtml::encode("T2");
        }
        if($this->date16 == 1) {
            $time_evening = 1;
            $schedule_evening.= ' '.CHtml::encode("T3");
        }
        if($this->date17 == 1) {
            $time_evening = 1;
            $schedule_evening.= ' '.CHtml::encode("T4");
        }
        if($this->date18 == 1) {
            $time_evening = 1;
            $schedule_evening.= ' '.CHtml::encode("T5");
        }
        if($this->date19 == 1) {
            $time_evening = 1;
            $schedule_evening.= ' '.CHtml::encode("T6");
        }
        if($this->date20 == 1) {
            $time_evening = 1;
            $schedule_evening.= ' '.CHtml::encode("T7");
        }
        if($this->date21 == 1) {
            $time_evening = 1;
            $schedule_evening.= ' '.CHtml::encode("CN");
        }

        if($time_evening == 1) {
            $schedule .= " Tối: ".$schedule_evening;
        }

        return $schedule;
    }
}